﻿namespace Pp.AdminPanel
{
    using System;
    using System.Linq;
    using System.Windows.Forms;
    using Core.FluentNH;
    using Repository;

    public partial class Frm_Kategori : BaseForm
    {
        public Frm_Kategori()
        {
            InitializeComponent();
        }

        private void Frm_Kategori_Load(object sender, EventArgs e)
        {
            UpdatePanels();
        }

        private void UpdatePanels()
        {
            var categories = ProjectCategoryRepository.GetAll().ToList();
            dataGridView1.DataSource = categories;
            comboBox1.Items.Clear();
            foreach (var projectCategory in categories)
            {
                comboBox1.Items.Add(new { projectCategory.Id, projectCategory.Name });
                comboBox1.ValueMember = "Id";
                comboBox1.DisplayMember = "Name";
            };
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Guid? parent = null;
            if (comboBox1.SelectedValue != null)
                parent = Guid.Parse(comboBox1.SelectedValue.ToString());
            var cat = ProjectCategoryRepository.Create(textBox1.Text, textBox2.Text, textBox3.Text, parent);
            if (cat.Success)
            {
                UpdatePanels();
            }
            else
            {
                MessageBox.Show(cat.Error);
            }
        }
    }

    public class BaseForm : Form
    {
        public UserRepository UserRepository { get; set; }
        public ProjectCategoryRepository ProjectCategoryRepository { get; set; }
        public GalleryRepository GalleryRepository { get; set; }
        public ProjectPhotoRepository ProjectPhotoRepository { get; set; }
        public ProjectRepository ProjectRepository { get; set; }
        public CommentRepository CommentRepository { get; set; }
        public AngelRepository AngelRepository { get; set; }
        public BaseForm()
        {
            var session = ConfigurationItem.Instance.OpenSession();
            session.Transaction.Begin();
            session.BeginTransaction();
            UserRepository = new UserRepository(session);
            ProjectCategoryRepository = new ProjectCategoryRepository(session);
            ProjectRepository = new ProjectRepository(session);
            GalleryRepository = new GalleryRepository(session);
            ProjectPhotoRepository = new ProjectPhotoRepository(session);
            CommentRepository = new CommentRepository(session);
            AngelRepository = new AngelRepository(session);
        }
    }
}
